import React from 'react'
import { useUserStore } from '@/store'
import { Navigate, useLocation } from 'react-router'

// 路径白名单
const WHITE_LIST = ['/login', '/test']

export default function AuthRoute({ children }) {
  const { token } = useUserStore()
  const location = useLocation()
  const path = location.pathname

  // 白名单路径直接访问
  if (WHITE_LIST.includes(path)) {
    return children
  }

  // 非白名单路径，校验token
  if (!token) {
    return <Navigate to="/login" state={{ from: location }} replace />
  }

  return children
}
